package Dao;

import eneity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    public static User getUserInfo(String username ,String password) throws SQLException {
        Connection  connection = DBUtil.getConnection();
        String sql = "select * from user where username = ? and password = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,username);
        statement.setString(2,password);

        ResultSet resultSet = null;
        resultSet = statement.executeQuery();
        User user = null;

        if (resultSet.next()){
            user = new User();
            user.setUserid(resultSet.getInt("userid"));
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
            System.out.println("查询数据成功! "+user.toString());
        }
        return user;
    }

    public static int insertUser(User user) throws SQLException {
        Connection connection = DBUtil.getConnection();
        String sql = "insert into user values(null,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,user.getUsername());
        statement.setString(2, user.getPassword());
        int ret = statement.executeUpdate();
        return ret;
    }

}
